package yl.hs.bmipfull.service.production.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.laboratory.IProductCateDao;
import yl.hs.bmipfull.daos.laboratory.IProductInfoDao;
import yl.hs.bmipfull.daos.production.IFinishProductInventoryDao;
import yl.hs.bmipfull.daos.production.IProductionDispatchLogDao;
import yl.hs.bmipfull.pojo.production.ProductionDispatchLogEntity;
import yl.hs.bmipfull.pojo.production.ProductionLineEntity;
import yl.hs.bmipfull.service.production.IProductionLogService;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.ArrayList;
import java.util.List;

/*
 * ProductionLogServiceImpl
 * @category
 * @author 谢景广
 * @date 2021-06-17
 */
@Service
public class ProductionLogServiceImpl extends ProductionBaseService implements IProductionLogService {

    @Autowired
    IProductionDispatchLogDao dao;
    @Autowired
    IFinishProductInventoryDao finishProductInventoryDao;
    @Autowired
    IProductCateDao cateDao;
    @Autowired
    IProductInfoDao infoDao;

    @Override
    public List<ProductionDispatchLogEntity> queryByDataSn(String dataSn) {

        return dao.queryByDataSN(dataSn);
    }

    @Override
    public PageResult<Object> queryCP(PageQuery pageQuery, int rowNum, int page) {
        try {
            pageQuery.checkSortSides(ProductionDispatchLogEntity.class);
            pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
            pageQuery.getQuery().put("pageSize", rowNum);
            PageResult<Object> result = new PageResult(rowNum);
            pageQuery.getQuery().put("unitSNArray", getCompanySNList());
            var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), ProductionLineEntity.class);
            result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
            result.setPage(page);
            var data = dao.queryByEntity(pageQuery.getQuery());
            List<Object> datas = new ArrayList<>();
            data.forEach(s -> {
                var results = s.toMap();
                var inventory = finishProductInventoryDao.findBySN(s.getDataSN());
                if (inventory != null) {
                    results.put("productName", inventory.getProductName());
                    results.put("productSN", inventory.getProductSN());
                    var info = infoDao.findBySN(inventory.getProductSN());
                    if (info != null) {
                        var cate = cateDao.findBySN(info.getProductCateSN());
                        if (cate != null) {
                            results.put("cateName", cate.getCateName());
                        }
                    }
                    results.put("packageName", inventory.getPackageName());
                    results.put("packWeight", inventory.getPackWeight());
                    results.put("packageSN", inventory.getPackageSN());
                    results.put("wareHouseName", inventory.getWareHouseName());
                    results.put("productName", inventory.getProductName());
                    datas.add(results);
                }
            });
            result.setData(datas);
            return result;
        }catch (Exception ee){
            throw ee;
        }
    }
}
